const express = require('express')
const router = express.Router()
const controllers = require('../controllers')
let interfaceList = []
const createRouter = item => {
  if (item.config) {
    return router[item.method || 'get'](item.url || '/', item.config, item.request)
  } else {
    return router[item.method || 'get'](item.url || '/', item.request)
  }
}
const createRoutes = obj => {
  Object.keys(obj).forEach(key => {
    if (obj[key] instanceof Array) {
      obj[key].forEach(item => {
        createRouter(item)
      })
    } else {
      createRoutes(obj[key])
    }
  })
}
createRoutes(controllers)
router.stack.forEach(item => {
  const { path, methods } = item.route
  interfaceList.push({
    path, methods
  })
})
router.get('/routes', (req, res) => {
  res.json({ routes: interfaceList })
})
module.exports = router
